<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>字典管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
</head>
<body>
<fieldset class="layui-elem-field" style="padding-top: 10px;">
    <from class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label" style="width: auto;">字典类型</label>
                <div class="layui-input-inline">
                    <select id="ddlDictType" name="dictType" lay-filter="dictType">
                    </select>
                </div>
            </div>

            <button class="layui-btn layui-btn-normal" data-type="doSearch" id="btnSearch">查询</button>
            <button class="layui-btn layui-btn-primary" data-type="doAdd" id="btnAdd">添加</button>
        </div>
    </from>
</fieldset>
<table class="layui-table" lay-filter="tbFliter" id="tbData" style="margin-top: -25px;">
</table>
<script src="../assets/layui/layui.js"></script>
<script type="text/html" id="barList">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.config({
        base: '../js/'
    });
    layui.use(['jquery','table','common','form'], function () {
        var table = layui.table,
            common=layui.common,
            form=layui.form,
            $ = layui.jquery,
            active = {
                doSearch: function () {
                    table.reload("tbOptions", {
                        where: {
                            dictType: $("#ddlDictType").val(),
                            page:1
                        }
                    });
                },
                doAdd: function () {
                    common.layer_show("字典新增",'DataDictEdit.html');
                }
            };

        //初始化下拉框
        common.initSelectData("#ddlDictType","common/list4Select?type=DDType&hasAll=1",function() {
            form.render();//刷新
        });

        var tbHeight = $(window).height() - 75;
        table.render({
            elem: '#tbData'
            , skin: 'row'
            , height: tbHeight
            , url: 'dict/list?span='+new Date().Format("yyyyMMddHHmmss")
            , page: true
            , cols: [[
                {field: 'dictTypeName', title: '类型', width: 160,}
                ,  {field: 'dictKey', title: 'key', width: 250}
                , {field: 'dictValue', title: '值', width: 350}
                , {field: 'operation', title: '操作', width: 200, toolbar: '#barList'}
            ]]
            , limits: [10, 20, 50]
            ,limit:20
            , where: {
                dictType: $("#ddlDictType").val(),
            }
            , id: 'tbOptions'
        });
        //table监听绑定
        table.on('tool(tbFliter)', function (obj) {
            var data = obj.data; //获得当前行数据
            var id = data.id;
            var layEvent = obj.event; //获得 lay-event
            if (layEvent === 'edit') { //编辑
                common.layer_show('字典编辑',"DataDictEdit.html?type=edit&id="+data.id);
            } else if (layEvent === 'del') { //删除
                layer.confirm('您确定删除选中字典吗', function (index) {
                    $.ajax({
                        type: 'POST',
                        url: 'dict/delete',
                        dataType: 'json',
                        data:{id:id},
                        success: function(rtn) {
                            if(rtn.code===0){
                                common.showMsg('操作成功!');
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            }
                            else{
                                common.showError("操作失败:" + rtn.msg);
                            }
                        },
                        error: function() {
                            common.showError("操作失败" );
                        }
                    });
                });
            }
        });
        //按钮事件绑定
        $('.layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>